/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmImage.h"
#include "prs_Unit.h"
using namespace prs;

// TRuleFrmImage //////////////////////////////////////////////////////////////

//
TRuleFrmImage::TRuleFrmImage(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmChildrenBase &RChildren,
  TRuleSetNodeIdOptionally &RSetNodeIdOptionally,
  TRuleSetAttrValue &RSetAttrValue,
  TRuleFrmGeom &RFrmGeom,
  TRuleSetStyle &RStyle
) 
:vParentNode(VParentNode)
,rGeometry(RFrmGeom)
{
  snt::text::TSequence T(Context);
  snt::text::TKeyword  K(Context);
  snt::text::TIdent    I(Context);
  snt::TFVoid F;
  TFuncNewNodeIntoAttrOldChildren FNewNode;
  snt::TReturn Return;

  this->Name(GetClassName()).Local(vParentNode)
  | K.New("image") & FNewNode.New(vParentNode,TNodeFrmImage()) 
    & RSetNodeIdOptionally.New() & RAttr.New() 
    & Return.New()
  ;
  
  RAttr
  | T.New("(") 
    & RAttr1.New() & RChildren.New() 
    & T.New(")") & Return.New()
  | Return.New()
  ;

  RAttr1
  | AAttr1.New()
  ;
  AAttr1
  | RSetAttrValue.New(
      VParentNode,
      TNodeFrm::attrName_Value,
      RSetAttrValue.SetFuncValueIdent(this,&TRuleFrmImage::FSetValue)
    )
    & AAttr1.New()

  | RStyle.New() & AAttr1.New()
  | F.New(this,&TRuleFrmImage::FSetGeometry) & RFrmGeom.New() & AAttr1.New()
  | Return.New()
  ;
}

//
bool TRuleFrmImage::FSetValue(TRuleSetAttrValue::TFuncValueIdentParams &Params)
{
  return TNodeDefTemplate::AddLinkAttrByParamId(Params.Attr,TFrmStringValue(),Params.Ident,Params.OutError);
}

//
bool TRuleFrmImage::FSetImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmImage* Image = dynamic_cast<TNodeFrmImage*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(Image)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	grf::TImageGdiP *image = Unit->Images.Array.
					GetImage( Params.Ident.GetValue().String() );
	if( !image ){
		Params.OutError << "Image " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	Image->Value.SetValue( image );
	return true;
}